package com.yhy.server.controller;

import com.yhy.domain.vo.PageResult;
import com.yhy.server.service.CommentService;
import com.yhy.server.service.SmallVideosService;
import com.yhy.server.service.VideoMQService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideoController {

    @Autowired
    private SmallVideosService videosService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private VideoMQService videoMQService;

    /** 视频上传
     *
     *  /smallVideos POST
     *
     * Body:
     *  videoThumbnail:视频封面图片
     *  videoFile:视频文件
     *
     * 返回值: null
     */
    @PostMapping
    public ResponseEntity uploadVideo(MultipartFile videoThumbnail,MultipartFile videoFile) throws IOException {
        return videosService.uploadVideo(videoThumbnail,videoFile);
    }

    /**
     * 小视频列表
     *
     *  路径:/smallVideos GET
     *
     *  参数:
     *  Query:
     *      page: 页数
     *      pagesize: 页尺寸
     *
     *  返回值: pageResult<VideoVo>
     */
    @GetMapping
    public ResponseEntity videoList(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pageResult = videosService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     *  关注视频作者
     *
     * 路径:
     *      /smallVideos/:uid/userFocus POST
     *
     * 参数:
     *      uid:用户uid
     *
     * 返回值: null
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable("uid") Long followUserId){
        return videosService.userFocus(followUserId);
    }

    /**
     * 取消关注视频作者
     *
     * 路径:
     *      /smallVideos/:uid/userUnFocus
     *
     * 参数:
     *      uid:用户uid
     *
     * 返回值: null
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("uid") Long followUserId){
        return videosService.userUnFocus(followUserId);
    }

    /**
     * 视频点赞
     *
     * 路径:
     *      /smallVideos/:id/like POST
     *
     * 参数:
     *      id:视频id
     *
     * 返回值: null
     *
     */
    @PostMapping("/{id}/like")
    public ResponseEntity likeVideo(@PathVariable("id") String videoId){
        videoMQService.sendMsg(videoId,1);
        return videosService.likeVideo(videoId);
    }

    /**
     * 视频点赞-取消
     *
     * 路径:
     *      /smallVideos/:id/dislike POST
     *
     * 参数:
     *      id:视频id
     *
     * 返回值: null
     *
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity dislikeVideo(@PathVariable("id") String videoId){
        videoMQService.sendMsg(videoId,3);
        return videosService.dislikeVideo(videoId);
    }

    /**
     * 评论列表
     *
     * 接口路径：GET/smallVideos/:id/comments
     *
     *  参数:
     *      id: 视频id
     *  Query:
     *      page: 页数
     *      pagesize: 页尺寸
     *
     *  返回值: PageResult<CommentVo>
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity comments(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize,
                                   @PathVariable("id") String videoId){
        return videosService.comments(page,pagesize,videoId);
    }

    /**
     * 评论发布
     *
     * 接口路径：POST/smallVideos/:id/comments
     *
     * 请求参数:
     *      id: 视频id
     *
     *  Body: comment: 评论正文
     *
     * 返回值: null
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity commitComments(@PathVariable("id") String videoId,@RequestBody Map map){
        String comment = (String) map.get("comment");
        videoMQService.sendMsg(videoId,2);
        return videosService.commitComments(videoId,comment);
    }
}